package cn.gzjp.modules.logis.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Map;

import cn.gzjp.common.utils.BaiduAPI;
import cn.gzjp.modules.logis.entity.LogisCar;
import cn.gzjp.modules.logis.service.LogisCarService;
import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import cn.gzjp.common.utils.DateUtils;
import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.common.utils.excel.ExportExcel;
import cn.gzjp.common.utils.excel.ImportExcel;
import cn.gzjp.modules.logis.entity.LogisCarRoad;
import cn.gzjp.modules.logis.service.LogisCarRoadService;

/**
 * 司机车辆路线Controller
 * @author zengcp
 * @version 2018-04-04
 */
@Controller
@RequestMapping(value = "${adminPath}/logis/logisCarRoad")
public class LogisCarRoadController extends BaseController {

	@Autowired
	private LogisCarRoadService logisCarRoadService;
	@Autowired
	private LogisCarService logisCarService;
	
	
	/**
	 * 司机车辆路线列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(LogisCarRoad logisCarRoad, String  openid,HttpServletRequest request, HttpServletResponse response, Model model) {
		if(StrUtil.isNotBlank(openid)){
			logisCarRoad.setOpenid(openid);
			model.addAttribute("logisCar", logisCarService.get(openid));
		}

		Page<LogisCarRoad> page = logisCarRoadService.findPage(new Page<LogisCarRoad>(request, response), logisCarRoad);
		model.addAttribute("page", page);
		model.addAttribute("openid", openid);

		return "modules/logis/logisCarRoadList";
	}

	/**
	 * 查看，增加，编辑司机车辆路线表单页面
	 */
	@RequestMapping(value = "form")
	public String form(LogisCarRoad logisCarRoad, String openid, Model model) {
		model.addAttribute("logisCarRoad", logisCarRoad);
		model.addAttribute("openid", openid);
		return "modules/logis/logisCarRoadForm";
	}

	/**
	 * 保存司机车辆路线
	 */
	@RequestMapping(value = "save")
	public String save(LogisCarRoad logisCarRoad, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if(!logisCarRoad.getIsNewRecord()){//编辑表单保存
			LogisCarRoad t = logisCarRoadService.get(logisCarRoad.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(logisCarRoad, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			int score = 75;
			Map<String, Double> startMap = BaiduAPI.getLngAndLat(t.getStartProvince()+t.getEndcity());
			Map<String, Double> endMap = BaiduAPI.getLngAndLat(t.getEndProvince()+t.getEndcity());
			String distanceStr = BaiduAPI.getDistance(startMap, endMap);
			if("0".equals(distanceStr)){
				distanceStr = BaiduAPI.getDistance(startMap, endMap);
			}
			int distance = Integer.parseInt(distanceStr);
			int price = (distance/10000)*2;
			if(price<100){
				price = price+20;
			}
			int dif = Integer.parseInt(t.getUnitPrice())-price;//差价
			if(dif<=0&&dif>=-10){
				score = score+(-dif);//
			}
			if(dif<-10&&dif>=-20){
				score = 10+score+(-dif/2);//
			}
			if(dif<-20&&dif>=-100){
				score = 20+score-((dif+20)/10);//
			}
			if(dif<-100){
				score=98;//
			}
			if(dif>0&&dif<=50){
				score = score-dif;
			}
			if(dif>50&&dif<=150){
				score = 25- ((dif-50)/5);
			}
			if(dif>150){
				score=9;
			}
			if(score>100){
				score=98;
			}
			if(score<9){
				score=9;
			}
			logisCarRoadService.save(t);
			logisCarRoadService.save(t);//保存
		}else{//新增表单保存

			int score = 75;
			Map<String, Double> startMap = BaiduAPI.getLngAndLat(logisCarRoad.getStartProvince()+logisCarRoad.getEndcity());
			Map<String, Double> endMap = BaiduAPI.getLngAndLat(logisCarRoad.getEndProvince()+logisCarRoad.getEndcity());
			String distanceStr = BaiduAPI.getDistance(startMap, endMap);
			if("0".equals(distanceStr)){
				distanceStr = BaiduAPI.getDistance(startMap, endMap);
			}
			int distance = Integer.parseInt(distanceStr);
			int price = (distance/10000)*2;
			if(price<100){
				price = price+20;
			}
			int dif = Integer.parseInt(logisCarRoad.getUnitPrice())-price;//差价
			if(dif<=0&&dif>=-10){
				score = score+(-dif);//
			}
			if(dif<-10&&dif>=-20){
				score = 10+score+(-dif/2);//
			}
			if(dif<-20&&dif>=-100){
				score = 20+score-((dif+20)/10);//
			}
			if(dif<-100){
				score=98;//
			}
			if(dif>0&&dif<=50){
				score = score-dif;
			}
			if(dif>50&&dif<=150){
				score = 25- ((dif-50)/5);
			}
			if(dif>150){
				score=9;
			}
			if(score>100){
				score=98;
			}
			if(score<9){
				score=9;
			}
			logisCarRoad.setScore(score);
			logisCarRoadService.save(logisCarRoad);//保存

			LogisCar logisCar = logisCarService.get(logisCarRoad.getOpenid());
			logisCar.setRoadCount(logisCar.getRoadCount()+1);
			logisCarService.save(logisCar);
		}
		addMessage(redirectAttributes, "保存司机车辆路线成功");
		return redirect("/logis/logisCarRoad/list?openid="+logisCarRoad.getOpenid());
	}
	
	/**
	 * 删除司机车辆路线
	 */
	@RequestMapping(value = "delete")
	public String delete(LogisCarRoad logisCarRoad, RedirectAttributes redirectAttributes) {
		LogisCar logisCar = logisCarService.get(logisCarRoad.getOpenid());
		logisCar.setRoadCount(logisCar.getRoadCount()-1);
		if(logisCar.getRoadCount()<0){
			logisCar.setRoadCount(0);
		}
		logisCarService.save(logisCar);
		logisCarRoadService.delete(logisCarRoad);
		addMessage(redirectAttributes, "删除司机车辆路线成功");
		return redirect("/logis/logisCarRoad/list?openid="+logisCarRoad.getOpenid());
	}
	
	/**
	 * 批量删除司机车辆路线
	 */
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			logisCarRoadService.delete(logisCarRoadService.get(id));
		}
		addMessage(redirectAttributes, "删除司机车辆路线成功");
		return redirect("/logis/logisCarRoad/?repage");
	}
	

	@ModelAttribute
	public LogisCarRoad get(@RequestParam(required=false) String id) {
		LogisCarRoad entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = logisCarRoadService.get(id);
		}
		if (entity == null){
			entity = new LogisCarRoad();
		}
		return entity;
	}
	

}